import { Component, Pipe, PipeTransform } from '@angular/core';

import { Events, AlertController, NavController, NavParams } from 'ionic-angular';

import { MarketingWeeklyDetailService } from './detail.service';

import { DomSanitizer } from '@angular/platform-browser';

import _ from 'lodash';

import {MarketingWeeklyEditPage} from "../edit/edit.component";
import {Common} from "../../../../providers/common";

@Component({
  selector: 'page-marketing-weekly-detail',
  templateUrl: './detail.html',
  providers: [MarketingWeeklyDetailService]
})
export class MarketingWeeklyDetailPage {
	moa_page_name: any = "page-marketing-weekly-detail";
  moa_page_explain: any = "工作周报详情";

  //工作周报业务对象
  detail = null;
  //工作日报列表
  chanelList = null;

  detailHtml5 = null;
  detailHtml6 = null;
  detailHtml7 = null;

  constructor(
    public alertCtrl: AlertController,
    public navCtrl: NavController,
    private sanitizer: DomSanitizer,
    private navParams: NavParams,
    public detailService: MarketingWeeklyDetailService,
    public events: Events
    ) {

    this.events.unsubscribe('weekly-edit:update');
    // 监听是否有更新操作
    this.events.subscribe('weekly-edit:update', (item,chanelListItem) => {
      console.info('---- weekly-edit:update:update------');
      this.detail = item;

      // this.displayHtml();

      this.chanelList = chanelListItem;
    });

    this.detail = this.navParams.get("detail");

    // this.displayHtml();

    this.detailService.getChanelListData(this.detail[0]).subscribe(data => {
      var dataList = [];
      _.forEach(data, function (i) {
        let d = i.split("|");
        d.push("false");
        dataList.push(d);
      });
      this.chanelList = dataList;
    });
  }

  /*ionViewDidLoad() {
    this.initDetailTextarea();
  }

  initDetailTextarea() {
    console.log(document.getElementById("tx_detail5").style);
    var el_tx_detail5 = document.getElementById("tx_detail5");
    let clientRects = el_tx_detail5.getClientRects();
    console.log(el_tx_detail5.getClientRects());
    el_tx_detail5.style["posHeight"] = clientRects[0].bottom;
  }*/

  /**
   * 以html输出显示
   */
  displayHtml() {
    if (this.detail[5]) {
      this.detailHtml5 = this.detail[5].replace(/<nbsp>/g, "&nbsp;");
    }
    if (this.detail[6]) {
      this.detailHtml6 = this.detail[6].replace(/<nbsp>/g, "&nbsp;");
    }
    if (this.detail[7]) {
      this.detailHtml7 = this.detail[7].replace(/<nbsp>/g, "&nbsp;");
    }

    this.detailHtml5 = this.sanitizer.bypassSecurityTrustHtml(this.detailHtml5);
    this.detailHtml6 = this.sanitizer.bypassSecurityTrustHtml(this.detailHtml6);
    this.detailHtml7 = this.sanitizer.bypassSecurityTrustHtml(this.detailHtml7);
  }

  /*
  * 跳转到编辑页面
  */
  goEditPage(item) {
    if(this.detail[10] == "0"){
         let alert = this.alertCtrl.create({
         title: "提示",
         message: "编辑字段太长，请到电脑版USP修改。",
         buttons: [
         {
           text: "确定",
           role: 'cancel',
           handler: () => {
           }
         }
         ]
       });
       alert.present();
    }else{
      this.detail[5] = this.detail[5].replace(/<nbsp>/g, " ");
      this.detail[5] = this.detail[5].replace(/<br\/>/g, "\n");
      this.detail[6] = this.detail[6].replace(/<nbsp>/g, " ");
      this.detail[6] = this.detail[6].replace(/<br\/>/g, "\n");
      this.detail[7] = this.detail[7].replace(/<nbsp>/g, " ");
      this.detail[7] = this.detail[7].replace(/<br\/>/g, "\n");

      this.navCtrl.push(MarketingWeeklyEditPage, {
        "edit" : item,"editmode":1
      });
    }
  }
  checkIsSelected(item) {
    item[item.length - 1] = !item[item.length - 1];
  }

  reSizeTextarea(obj) {
    console.log(obj);
    obj.style.posHeight = obj.scrollHeight;
  }
}

/**
 * 数据显示格式化管道
 */
@Pipe({
  name: 'formatTextToHtml'
})
export class FormatTextToHtml implements PipeTransform {

  constructor(public common: Common) {}

  transform(exponent: string): string {
    // console.log(params, exponent);
    let returnData = exponent;
    if (returnData) {
      returnData = _.replace(returnData, /\r{0,}\n/g, "<br/>");
      returnData = _.replace(returnData, /\n{0,}\r/g, "<br/>");
      returnData = _.replace(returnData, /\s/g, "&nbsp;");
    }

    return returnData;
  }
}
